package com.example.onlineeducation.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.onlineeducation.base.BaseViewModel
import com.example.onlineeducation.data.db.datastore.UserInfoDataStoreKey
import com.example.onlineeducation.data.entity.OfflineStudyLiveDetailEntity
import com.example.onlineeducation.repository.OfflineRepository
import com.yc.jetpacklib.data.datastore.YcDataStore
import com.yc.jetpacklib.extension.get
import com.yc.jetpacklib.net.YcResult
import kotlinx.coroutines.flow.collectLatest

/**
 * Creator: yc
 * Date: 2022/5/5 13:43
 * UseDes:
 */
class OfflineStudyDetailAcVM : BaseViewModel() {
    private val mOfflineRepository: OfflineRepository by lazy { OfflineRepository(mApiService) }


    private val _mStudyLiveDetail = MutableLiveData<YcResult<OfflineStudyLiveDetailEntity>>()
    val mStudyLiveDetail: LiveData<YcResult<OfflineStudyLiveDetailEntity>> = _mStudyLiveDetail

    fun getOfflineStudyLiveDetail(proId: String, studyLiveId: String) = ycLaunch {
        mOfflineRepository.getOfflineStudyLiveDetail(proId, studyLiveId).ycHasLoading().collectLatest { result ->
            _mStudyLiveDetail.postValue(result)
        }
    }

    private val _mSignResult = MutableLiveData<YcResult<String>>()
    val mSignResult: LiveData<YcResult<String>> = _mSignResult

    fun offlineStudyLiveSignInOrSignOut(
        studyLiveId: String,
        address: String,
        lat: String,
        lng: String,
        signType: String
    ) = ycLaunch {
        YcDataStore.getMore {
            val userName = it[UserInfoDataStoreKey.KEY_USER_NAME]!!
            val userPhone = it[UserInfoDataStoreKey.KEY_USER_PHONE_NUM]!!
            mOfflineRepository.offlineStudyLiveSignInOrSignOut(studyLiveId, address, lat, lng, userName, userPhone, signType).ycHasLoading()
                .collectLatest { result ->
                    _mSignResult.postValue(result)
                }
        }
    }

}